/**
 * @file 工单操作-打款凭证
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.extend({
    list_modify_common: "work/modify/common/zmn.list.modify.common",
}).define(['layer', 'list_modify_common', 'form', 'upload', 'zmnUtils', '$http','$api'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var config = layui.oms_work_config;
    var opCommonUtils = layui.list_modify_common;
    let upload = layui.upload;

    var zmnUtils = layui.zmnUtils;
    var $http = layui.$http;
    let timestamp = new Date().getTime();

    var SAVE_REMIT_VOUCHER_URL = '/work/modify/save/remitVoucher.action';

    /**
     * 初始化页面
     */
    function initPage () {

        //验证
        defineVerify();

        // 初始化文件上传插件
        for (let i = 0; i < 1; i++) {
            uploaderImage("imageSrcUrl" + i, "imageSrcUrl" + i + "Value", i);
        }


        // 回显图片&凭证号
        let refundVoucherData = remitVoucherVO.refundVoucherData;
        if (!(refundVoucherData == null)) {
            refundVoucherData.forEach(function (data, index) {
                reLoadImage("imageSrcUrl" + index, data.imageUrl);
                $("#imageSrcUrl" + index + "Value").val(data.imageUrl);
                $("#imageSrcUrl" + index + "Values").attr("src", data.imageUrl).attr("style", "width: 0;height: 0");
                $('#refund_no_input' + index).val(data.refundNo);
                $('#refund_no_input' + index).attr('lay-verify', 'required|refundNo');
            });
        }
    }

    let indexUploadImags;

    function uploaderImage(elemId, valueId, index) {
        upload.render({
            elem: '#' + elemId
            , exts: 'jpg|png|jpeg'
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/work/modify/uploader/orderPhoto?timestamp=' + timestamp + '&elemId=' + elemId
            , size: 5120
            , beforeSend: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.statusBool) {
                    let data = res.data;
                    $("#" + valueId).val(data);
                    $("#" + valueId + "s").attr("src", data).attr("style", "width: 0;height: 0");
                    reLoadImage(elemId, data);
                    $('#refund_no_input' + index).attr('lay-verify', 'required|refundNo');
                } else {
                    layer.msg(res.message);
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
        let next = $("#" + elemId + "Value").next();
        let next1 = $(next).next();
        $(next).attr("style", "visibility:hidden !important");
        $(next1).attr("style", "visibility:hidden !important");
    }

    function getRefundVoucherData() {
        let refundVoucherData = [];
        for (let i = 0; i < 1; i++) {
            var imageUrl = $('#imageSrcUrl' + i + 'Value').val();
            if (imageUrl) {
                var refundNo = $('#refund_no_input' + i).val().replace(/\s+/g, "");
                refundVoucherData.push({imageUrl: imageUrl, refundNo: refundNo})
            }
        }
        return refundVoucherData;
    }
    
    function saveRefundVoucher (params, elem) {
        $http.lockSave(SAVE_REMIT_VOUCHER_URL, params, elem).then(function (data) {
            zmnUtils.layui.toastSuccess('操作成功', function () {
                opCommonUtils.goSomeWorkDetail(data);
            });
        }, function (msg) {
            zmnUtils.layui.toastError(msg || '操作失败');
        })
    }

    /**
     * 验证
     */
    function defineVerify(){
        form.verify({
            refundNo: function (val) {
                if (val.toString().length > 50) {
                    return "最多输入50位";
                }
            }
        })
    }

    /**
     * 初始化页面事件
     */
    function initEvent () {
        // 监听表单提交
        form.on("submit()",function (obj) {
            var formData = obj.field;
            let refundVoucherData = getRefundVoucherData();
            if (refundVoucherData.length === 0) {
                layer.msg('至少上传一张退款凭证！');
                return false;
            }
            formData.refundVoucherData = refundVoucherData;
            // 保存
            saveRefundVoucher(formData, obj.elem);
            return false;
        });


        // 点击放大
        $(document).on('click', '.btn-review', function () {
            let $container = $(this).parent().prev();
            $container.viewer().on('hide', function () {
                $container.viewer('destroy');
            });

            let index = $(this).index();
            $container.viewer('view', index);

        });
        // 点击删除
        $(document).on('click', '.btn-delete', function () {
            let $uploadDiv = $(this).parent().prev();
            $uploadDiv.find(".layui-upload-drag").attr("style", "");
            $uploadDiv.find("i").attr("style", "");
            $uploadDiv.find("p").attr("style", "");
            $uploadDiv.find(":hidden").val("");

            let index = $(this).data('index');
            $('#refund_no_input' + index).attr('lay-verify', '');
        });

        // 打款银行选择框
        form.on('select(remitBankChange)', function(data){
            let bank = data.elem.selectedOptions[0].getAttribute('data-bank')
            let account = data.elem.selectedOptions[0].getAttribute('data-account')
            $('#payerThirdAccountBank').val(bank);
            $('#payerThirdAccount').val(account);
        });

    }


    // 程序入口
    function init () {
       // 初始化页面
       initPage();
       // 初始化事件
       initEvent();
    }
    // 初始化
    init();

    exports('oms_work_modify_remit_voucher');
});
